package com.yueqian.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yueqian.framework.entity.BaseEntity;
import com.yueqian.system.converter.XxxConvert;
import com.yueqian.system.entity.Xxx;
import com.yueqian.system.mapper.XxxMapper;
import com.yueqian.system.service.XxxService;
import com.yueqian.system.vo.req.XxxReqVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class XxxServiceImpl extends ServiceImpl<XxxMapper, Xxx> implements XxxService {
	@Override
	public Page<Xxx> selectPageData(XxxReqVO reqVO) {
		IPage<Xxx> page = reqVO.getPage(reqVO.getCurrentPage(), reqVO.getPageSize());
		Wrappers.lambdaQuery();
		return (Page<Xxx>) this.baseMapper.selectPage(page, Wrappers.lambdaQuery(Xxx.class)
																	.eq(StrUtil.isNotBlank(reqVO.getXxx()), Xxx::getXxx, reqVO.getXxx()));
	}

	@Override
	public List<Xxx> selectListData(XxxReqVO reqVO) {
		return this.baseMapper.selectList(Wrappers.lambdaUpdate(Xxx.class)
												  .in(CollectionUtil.isNotEmpty(reqVO.getIds()), BaseEntity::getId, reqVO.getIds()));

	}

	@Transactional
	@Override
	public Boolean add(List<XxxReqVO> reqVO) {
		if (CollectionUtil.isEmpty(reqVO)) {
			return false;
		}
		List<Xxx> list = XxxConvert.INSTANCE.toList(reqVO);
		return this.saveBatch(list);
	}

	@Override
	public Long add(XxxReqVO reqVO) {
		Xxx entity = XxxConvert.INSTANCE.toEntity(reqVO);
		//编辑
		if (ObjectUtil.isNotNull(entity.getId())) {
			this.saveOrUpdate(entity);
		}
		//新增
		this.saveOrUpdate(entity);
		return entity.getId();
	}

	@Transactional
	@Override
	public Boolean delete(List<Long> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return false;
		}
		return this.removeByIds(ids);
	}

	@Override
	public Long delete(Long id) {
		this.removeById(id);
		return id;
	}

	@Override
	public List<Xxx> getDetail(List<Long> ids) {
		if (CollectionUtil.isNotEmpty(ids)) {
			ArrayList<Xxx> xxxList = new ArrayList<>();
			xxxList.add(new Xxx());
			return xxxList;
		}
		return this.listByIds(ids);
	}

	@Override
	public Xxx getDetail(Long id) {
		return this.getById(id);
	}
}
